package com.nhjf.admin.controller.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nhjf.admin.controller.BaseController;
import com.nhjf.admin.util.Jurisdiction;
import com.nhjf.command.organization.OrganizationCommand;
import com.nhjf.common.page.Pagination;
import com.nhjf.model.cn_nojd.Cnnojd;
import com.nhjf.model.system.Organization;
import com.nhjf.model.system.Resource;
import com.nhjf.model.system.User;
import com.nhjf.model.teachers.OperationLog;
import com.nhjf.model.ztreeModel.ZtreeModel;
import com.nhjf.pojo.dto.result.ServiceResultDto;
import com.nhjf.pojo.qo.cn_nojd.CnnojdQo;
import com.nhjf.pojo.qo.system.OrganizationQo;
import com.nhjf.pojo.qo.system.ResourceQo;
import com.nhjf.service.cn_nojd.CnnojdService;
import com.nhjf.service.system.OrganizationService;
import com.nhjf.service.teachers.OperationLogService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @类功能说明：机构管理
 * @部门：技术部
 * @作者：龚一凡
 * @版本：V1.0
 */
@Controller
@RequestMapping(value = "/organization")
public class OrganizationController extends BaseController{
	@Autowired
	private OrganizationService organizationService;
	@Autowired
	private OperationLogService operationLogService;
	@Autowired
	private CnnojdService cnnojdService;

	/**
	 * 
	 * @方法功能说明：机构列表
	 * @修改者名字：龚一凡
	 * @修改内容：
	 * @参数：
	 * @return:
	 * @throws
	 */
	@RequestMapping(value = "/list")
	public String queryUserList(HttpServletRequest request,@ModelAttribute OrganizationQo organizationQo, Model model,@RequestParam(value="page", defaultValue="1")Integer page){



		if (organizationQo == null) {
			organizationQo = new OrganizationQo();
		}
		Map<String ,String>address=new HashMap<String, String>();

		
		Pagination pagination = new Pagination();
		pagination.setPageSize(10);
		pagination.setCondition(organizationQo);
		pagination.setPageNo(page);
		pagination = organizationService.queryPagination(pagination);
		List<Organization> organization=(List<Organization>) pagination.getList();
		for (Organization organization2 : organization) {
			//省
			if(organization2.getProvince()!=null) {
				Cnnojd province= cnnojdService.get(organization2.getProvince());
				organization2.setProvinces(province.getName());
			}else {
				address.put("Province", "");
			}
			if(organization2.getCity()!=null) {
				Cnnojd Addresscity= cnnojdService.get(organization2.getCity());
				organization2.setCitys(Addresscity.getpName());
			}else {
				address.put("City", "");
			}
			//区
			if(organization2.getCounty()!=null) {
				Cnnojd County= cnnojdService.get(organization2.getCounty());
				organization2.setCountys(County.getpName());
			}else {
				address.put("County", "");
			}
			pagination.setList(organization);
		}
		model.addAttribute("pagination", pagination);
		return "/organization/organization_list.html";
	}
	@RequestMapping(value = "/organization_toadd")
	public String toAdd(HttpServletRequest request,Model model){
		CnnojdQo qo=new CnnojdQo();
		qo.setpId(0L);
		List<Cnnojd>  cnnojd=cnnojdService.queryList(qo);
		model.addAttribute("cnnojd",cnnojd);
		return "/organization/organization_add.html";
	}
	/**
	 * 新增机构
	 * @param request
	 * @param model
	 * @param organizationQo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/organization_add")
	public String add(HttpServletRequest request,Model model,OrganizationCommand command){

		if(command==null) {
			return "500";
		}
		if(StringUtils.isEmpty(command.getName())) {
			return "500";
		}
		User user=(User)Jurisdiction.getSession().getAttribute(com.nhjf.common.Constants.USER_INFO);
		command.setCreateDate(new Date());
		command.setStatus(1L);
		command.setCreateUser(user.getId());
		organizationService.svaeCommand(command);
		OperationLog operationLog=new OperationLog();
		operationLog.setOperationType(2);
		operationLog.setOperationDescription("新增机构!");
		operationLog.setUserId(user);
		operationLog.setOperationTime(new Date());
		operationLog.setOperationUserName(user.getUserName());
		operationLogService.save(operationLog);
		return "200";
	}
	@RequestMapping("/tz_organization_update")
	public String tz_organization_update(String id,Model model) {
		CnnojdQo qo=new CnnojdQo();
		qo.setpId(0L);
		List<Cnnojd>  cnnojd=cnnojdService.queryList(qo);
		model.addAttribute("cnnojd",cnnojd);
		OrganizationQo organizationQo=new OrganizationQo();
		organizationQo.setOrganizationid(Long.parseLong(id));
		Organization organization= organizationService.queryUnique(organizationQo);
		model.addAttribute("organization",organization);
		return "/organization/organization_update.html";
	}
	@RequestMapping("/organization_update")
	@ResponseBody
	public String organization_Update(OrganizationCommand command) {
		if(command==null) {
			return "500";
		}
		ServiceResultDto dto= organizationService.modifyCommand(command);
		if(dto.getSuccess()) {
			return "200";
		}
		return "500";
		
	}
	//三级联动
	@ResponseBody
	@RequestMapping("/Three_level_linkage")
	public String Threelevellinkage(String pid) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		if(StringUtils.isEmpty(pid)) {
			CnnojdQo qo=new CnnojdQo();
			qo.setpId(0L);
			List<Cnnojd>  cnnojd=cnnojdService.queryList(qo);
			resMap.put("code", "200");
			resMap.put("message", cnnojd);
			return JSONObject.fromObject(resMap).toString();
		}else {
			CnnojdQo qo=new CnnojdQo();
			qo.setpId(Long.parseLong(pid));
			List<Cnnojd>  cnnojd=cnnojdService.queryList(qo);
			resMap.put("code", "200");
			resMap.put("message", cnnojd);
			return JSONObject.fromObject(resMap).toString();
		}
	}
}
